package cn.stylefeng.guns.sys.modular.system.controller;

import cn.hutool.core.util.ZipUtil;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.sys.core.util.JsonResult;
import cn.stylefeng.guns.sys.core.util.QiniuUtil;
import cn.stylefeng.guns.sys.modular.system.entity.BtySzgl;
import cn.stylefeng.guns.sys.modular.system.model.params.BtySzglParam;
import cn.stylefeng.guns.sys.modular.system.service.BtySzglService;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;


/**
 * 双证管理控制器
 *
 * @author yxc
 * @Date 2022-03-16 10:17:47
 */
@Controller
@RequestMapping("/btySzgl")
public class BtySzglController extends BaseController {

    private String PREFIX = "modular/system/btySzgl";

    @Autowired
    private BtySzglService btySzglService;

    /**
     * 跳转到主页面
     *
     * @author yxc
     * @Date 2022-03-16
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "/btySzgl.html";
    }

    /**
     * 新增页面
     *
     * @author yxc
     * @Date 2022-03-16
     */
    @RequestMapping("/add")
    public String add() {
        return PREFIX + "/btySzgl_add.html";
    }

    /**
     * 编辑页面
     *
     * @author yxc
     * @Date 2022-03-16
     */
    @RequestMapping("/edit")
    public String edit() {
        return PREFIX + "/btySzgl_edit.html";
    }

    /**
     * 新增接口
     *
     * @author yxc
     * @Date 2022-03-16
     */
    @RequestMapping("/addItem")
    @ResponseBody
    public ResponseData addItem(BtySzglParam btySzglParam) {
        this.btySzglService.add(btySzglParam);
        return ResponseData.success();
    }

    /**
     * 编辑接口
     *
     * @author yxc
     * @Date 2022-03-16
     */
    @RequestMapping("/editItem")
    @ResponseBody
    public ResponseData editItem(BtySzglParam btySzglParam) {
        this.btySzglService.update(btySzglParam);
        return ResponseData.success();
    }

    /**
     * 删除接口
     *
     * @author yxc
     * @Date 2022-03-16
     */
    @RequestMapping("/delete")
    @ResponseBody
    public ResponseData delete(BtySzglParam btySzglParam) {
        this.btySzglService.delete(btySzglParam);
        return ResponseData.success();
    }

    /**
     * 查看详情接口
     *
     * @author yxc
     * @Date 2022-03-16
     */
    @RequestMapping("/detail")
    @ResponseBody
    public ResponseData detail(BtySzglParam btySzglParam) {
        BtySzgl detail = this.btySzglService.getById(btySzglParam.getId());
        return ResponseData.success(detail);
    }

    /**
     * 查询列表
     *
     * @author yxc
     * @Date 2022-03-16
     */
    @ResponseBody
    @RequestMapping("/list")
    public LayuiPageInfo list(BtySzglParam btySzglParam) {
        return this.btySzglService.findPageBySpec(btySzglParam);
    }


    /**
     * 导入excel
     */
    @RequestMapping("/import")
    @ResponseBody
    public Object batchImport(@RequestParam("file") MultipartFile file) {

        String fileName = file.getOriginalFilename();//文件名
        Boolean flag;
        JsonResult jsonResult = new JsonResult();
        try {
            flag = btySzglService.batchImport(fileName, file);
        } catch (Exception e) {
            e.printStackTrace();
            jsonResult.setState(0);
            jsonResult.setErrmsg(e.getMessage());
            return jsonResult;
        }
        return flag;
    }

    /**
     * 导出excel
     */
    @ResponseBody
    @RequestMapping("/expList")
    public void expList(BtySzglParam btySzglParam, HttpServletResponse response) {
        this.btySzglService.expList(btySzglParam, response);
    }

    /**
     *营运经理列表
     * @return
     */
    @ResponseBody
    @RequestMapping("/yyjlList")
    public ResponseData yyjlList(){
        return this.btySzglService.yyjlList();
    }

    /**
     * 批量下载图片
     */
    @RequestMapping(value = "/btnDown")
    @ResponseBody
    public Object btnDown(@RequestParam("ids") String ids, HttpServletRequest request, HttpServletResponse response) throws Exception {
        this.btySzglService.btnDown(ids,request,response);
        String urls="D:/szgl/sztp/";
        ZipUtil.zip(urls);
        return SUCCESS_TIP;
    }

    @RequestMapping(value = "/uploadImg")
    @ResponseBody
    public Map<String, Object> uploadImg(@RequestPart("file") MultipartFile file, HttpServletRequest request) {
        Map<String, Object> msg = new HashMap<>(16);
        if (file.isEmpty()) {
            msg.put("code", 1);
            msg.put("msg", "上传失败");
            return msg;
        }
        try {
            byte[] bytes = file.getBytes();
            String key = file.getOriginalFilename();
            String imageName = UUID.randomUUID().toString();
            Map<String, Object> data = new HashMap<>(16);

            try {
                //上传到七牛云
                String url = QiniuUtil.upload(bytes, imageName);
                //0表示成功，1失败
                msg.put("code", 0);
                msg.put("msg", "上传成功");
                msg.put("data", data);
                data.put("src", url);
            } catch (Exception e) {
                e.printStackTrace();
            }
            return msg;
        } catch (IOException e) {
            msg.put("code", 1);
            msg.put("msg", "上传失败");
            return msg;
        }
    }

}


